//
// Created by 32827 on 2023/11/29.
//
#include "yz_spi.h"
void SPI1_Handler(YZF_GLOBAL_INDEX index){
    if(yzf_spi_flag_rxne(SPI1)){

    }
    if(yzf_spi_flag_txe(SPI1)){

    }
    if (yzf_spi_flag_ovr(SPI1)){
        printf("SPI 溢出错误\n");
    }
    if(yzf_spi_flag_crcerr(SPI1)){
        printf("SPI CRC错误标志\n");
    }
    if(yzf_spi_flag_modf(SPI1)){
        printf("SPI 主模式失效\n");
    }
}
YZF_RESULT yz_spi_init_full_lines(YZF_SPI_POINTER spi){
    yzf_spi_open_source_clock(spi);

    yzf_spi_disable(spi);

    yzf_spi_set_cpol_HIGH(spi);
    yzf_spi_set_cpha_second(spi);

    yzf_spi_set_master(spi);

    yzf_spi_set_br(spi,YZF_SPI_PLCK_256);

    yzf_spi_enable_ssm(spi);
    yzf_spi_select_ssi(spi);

    yzf_spi_set_MSBFIRST(spi);

    yzf_spi_disable_RXONLY(spi);
    yzf_spi_set_DFF_8bits(spi);

    spi->CR2 = 0;

    yzf_spi_enable(spi);
    return YZF_OK;
}
YZF_RESULT yz_spi_init_full_lines_ex(YZF_SPI_POINTER spi,YZF_SPI_BR br,YZF_BOOL cpol,YZF_BOOL cpha) {
    yzf_spi_open_source_clock(spi);

    yzf_spi_disable(spi);

    if (cpol) {
        yzf_spi_set_cpol_HIGH(spi);
    } else {
        yzf_spi_set_cpol_LOW(spi);
    }

    if (cpha) {
        yzf_spi_set_cpha_second(spi);
    } else {
        yzf_spi_set_cpha_first(spi);
    }

    yzf_spi_set_master(spi);

    yzf_spi_set_br(spi, br);

    yzf_spi_enable_ssm(spi);
    yzf_spi_select_ssi(spi);

    yzf_spi_set_MSBFIRST(spi);

    yzf_spi_disable_RXONLY(spi);
    yzf_spi_set_DFF_8bits(spi);

    spi->CR2 = 0;

    yzf_spi_enable(spi);
    return YZF_OK;
}
YZF_RESULT yz_spi_init_full_hard_lines_ex(YZF_SPI_POINTER spi,YZF_SPI_BR br,YZF_BOOL cpol,YZF_BOOL cpha) {
    yzf_spi_open_source_clock(spi);

    yzf_spi_disable(spi);

    if (cpol) {
        yzf_spi_set_cpol_HIGH(spi);
    } else {
        yzf_spi_set_cpol_LOW(spi);
    }

    if (cpha) {
        yzf_spi_set_cpha_second(spi);
    } else {
        yzf_spi_set_cpha_first(spi);
    }

    yzf_spi_set_master(spi);

    yzf_spi_set_br(spi, br);

//    yzf_spi_enable_ssm(spi);
//    yzf_spi_select_ssi(spi);

    yzf_spi_set_MSBFIRST(spi);

    yzf_spi_disable_RXONLY(spi);
    yzf_spi_set_DFF_8bits(spi);

    spi->CR2 = 0;

    yzf_spi_enable(spi);
    return YZF_OK;
}
YZF_RESULT yz_spi_init16_full_lines_ex(YZF_SPI_POINTER spi,YZF_SPI_BR br,YZF_BOOL cpol,YZF_BOOL cpha){
    yzf_spi_open_source_clock(spi);

    yzf_spi_disable(spi);

    if (cpol) {
        yzf_spi_set_cpol_HIGH(spi);
    } else {
        yzf_spi_set_cpol_LOW(spi);
    }

    if (cpha) {
        yzf_spi_set_cpha_second(spi);
    } else {
        yzf_spi_set_cpha_first(spi);
    }

    yzf_spi_set_master(spi);

    yzf_spi_set_br(spi, br);

    yzf_spi_enable_ssm(spi);
    yzf_spi_select_ssi(spi);

    yzf_spi_set_MSBFIRST(spi);

    yzf_spi_disable_RXONLY(spi);
    yzf_spi_set_DFF_16bits(spi);

    spi->CR2 = 0;

    yzf_spi_enable(spi);
    return YZF_OK;
}
YZF_RESULT yz_spi_transmit_full_lines(YZF_SPI_POINTER spi,uint8_t* data,uint16_t len){
    int i=0;
    while (i<len)
    {
        while (yzf_spi_flag_txe(spi)==YZF_FALSE) {}
        yzf_spi_write_byte(spi,*(data+i));
        while (yzf_spi_flag_rxne(spi)==YZF_FALSE){};
        yzf_spi_read_byte(spi);
        i++;
    }
    while (yzf_spi_flag_txe(spi)==YZF_FALSE) {}
    while (yzf_spi_flag_bsy(spi)==YZF_TRUE) {}
    uint8_t temp = SPI1->DR;
    temp = SPI1->SR;
    return YZF_OK;
}
YZF_RESULT yz_spi_recive_full_lines(YZF_SPI_POINTER spi,uint8_t* data,uint16_t len){
    while (len)
    {
        // wait for BSY bit to Reset -> This will indicate that SPI is not busy in communication
        while (yzf_spi_flag_txe(spi)==YZF_FALSE) {}
        yzf_spi_write_byte(spi,*(data));  // send dummy data
        while (yzf_spi_flag_rxne(spi)==YZF_FALSE){};  // Wait for RXNE to set -> This will indicate that the Rx buffer is not empty
        *data++ = yzf_spi_read_byte(spi);
        len--;
    }
    return YZF_OK;
}
uint32_t count=0;
void yz_spi_recive_transmit_full_lines_start(YZF_SPI_POINTER spi){
    yzf_spi_enable(spi);
}
uint8_t yz_spi_recive_transmit_full_lines(YZF_SPI_POINTER spi,uint8_t data){
    count=0;
    while (yzf_spi_flag_txe(spi)==YZF_FALSE||count>10000) {count++;}
    yzf_spi_write_byte(spi,data);
    count=0;
    while (yzf_spi_flag_rxne(spi)==YZF_FALSE||count>10000){count++;}
    data = yzf_spi_read_byte(spi);
    return data;
}
uint16_t yz_spi_recive16_transmit_full_lines(YZF_SPI_POINTER spi,uint16_t data){
    count=0;
    while (yzf_spi_flag_txe(spi)==YZF_FALSE||count>10000) {count++;}
    yzf_spi_write_word(spi,data);
    count=0;
    while (yzf_spi_flag_rxne(spi)==YZF_FALSE||count>10000){count++;}
    data = yzf_spi_read_word(spi);
    return data;
}
void yz_spi_recive_transmit_full_lines_end(YZF_SPI_POINTER spi){
    count=0;
    while (yzf_spi_flag_txe(spi)==YZF_FALSE||count>10000) {count++;}
    count=0;
    while (yzf_spi_flag_bsy(spi)==YZF_TRUE||count>10000) {count++;}
    uint8_t temp = spi->DR;
    temp = spi->SR;
    yzf_spi_disable(spi);
}

YZF_RESULT yz_spi_init_normal(YZF_SPI_POINTER spi){
    yzf_spi_open_source_clock(spi);

    yzf_spi_set_br(spi,YZF_SPI_PLCK_4);

    yzf_spi_set_cpol_HIGH(spi);
    yzf_spi_set_cpha_first(spi);

    yzf_spi_enable_ssm(spi);
    yzf_spi_select_ssi(spi);

    yzf_spi_set_MSBFIRST(spi);
    yzf_spi_crc_disable(spi);

    yzf_spi_set_DFF_8bits(spi);
    yzf_spi_set_crc_poly(spi,10);

    yzf_spi_enable_err_interrupt(spi);
    yz_int_set_SPI1( SPI1_Handler,YZF_GLOBAL_SHEET_ERROR_INDEX);
    yzf_spi_disable_rxne_interrupt(spi);
    yzf_spi_disable_txe_interrupt(spi);

    yzf_spi_disable_RXONLY(spi);
    yzf_spi_disable_BIDI(spi);
    yzf_spi_set_master(spi);
    return YZF_OK;
}
YZF_RESULT yz_spi_transmit_normal(YZF_SPI_POINTER spi,uint8_t* data,uint16_t len){
#if YZ_USE_WITH_HAL
    switch ((uint32_t)spi) {
        case (uint32_t)SPI1:
            HAL_SPI_Transmit(yz_hal_get_spi1(),data,len,1000);
            return YZF_OK;
        default:
            return YZF_NOT_FOUND;
    }
#else
    yzf_spi_enable(spi);
    for(uint16_t i=0;i<len;i++){
        while (yzf_spi_flag_txe(spi)==YZF_FALSE);
        yzf_spi_write_byte(spi,*(data+i));
    }
    while (yzf_spi_flag_txe(spi)==YZF_FALSE);
    while (yzf_spi_flag_bsy(spi)==YZF_TRUE);
    yzf_spi_disable(spi);
#endif
    return YZF_OK;
}
YZF_RESULT yz_spi_recive_normal(YZF_SPI_POINTER spi,uint8_t* data,uint16_t len){
    yzf_spi_enable(spi);
    for(uint16_t i=0;i<len;i++){
        while (yzf_spi_flag_rxne(spi)==YZF_FALSE);
        *(data+i) = yzf_spi_read_byte(spi);
    }
    while (yzf_spi_flag_rxne(spi)==YZF_FALSE);
    while (yzf_spi_flag_bsy(spi)==YZF_TRUE);
    yzf_spi_disable(spi);
    return YZF_OK;
}

YZF_RESULT yz_spi_init_bidi(YZF_SPI_POINTER spi){
    yzf_spi_open_source_clock(spi);

    yzf_spi_set_br(spi,YZF_SPI_PLCK_4);

    yzf_spi_set_cpol_HIGH(spi);
    yzf_spi_set_cpha_first(spi);

    yzf_spi_enable_ssm(spi);
    yzf_spi_select_ssi(spi);

    yzf_spi_set_MSBFIRST(spi);
    yzf_spi_crc_disable(spi);

    yzf_spi_set_DFF_8bits(spi);
    yzf_spi_set_crc_poly(spi,10);

    yzf_spi_disable_err_interrupt(spi);
    yz_int_set_SPI1( SPI1_Handler,YZF_GLOBAL_SHEET_ERROR_INDEX);
    yzf_spi_disable_rxne_interrupt(spi);
    yzf_spi_disable_txe_interrupt(spi);

    yzf_spi_disable_RXONLY(spi);
    yzf_spi_enable_BIDI(spi);
    yzf_spi_set_master(spi);
    return YZF_OK;
}
YZF_RESULT yz_spi_transmit_bidi(YZF_SPI_POINTER spi,uint8_t* data,uint16_t len){
    yzf_spi_enable_BIDIO(spi);
    yzf_spi_enable(spi);
    yzf_spi_write_byte(spi,*data);
    for(uint16_t i=1;i<len;i++){
        while (yzf_spi_flag_txe(spi)==YZF_FALSE);
        yzf_spi_write_byte(spi,*(data+i));
    }
    while (yzf_spi_flag_txe(spi)==YZF_FALSE);
    while (yzf_spi_flag_bsy(spi)==YZF_TRUE);
    yzf_spi_disable(spi);
    yzf_spi_disable_BIDIO(spi);
    return YZF_OK;
}
YZF_RESULT yz_spi_recive_bidi(YZF_SPI_POINTER spi,uint8_t* data,uint16_t len){
    yzf_spi_enable_RXONLY(spi);
    yzf_spi_enable(spi);
    for(uint16_t i=0;i<len;i++){
        while (yzf_spi_flag_rxne(spi)==YZF_FALSE);
        *(data+i) = yzf_spi_read_byte(spi);
    }
    yzf_spi_disable(spi);
    yzf_spi_disable_RXONLY(spi);
    return YZF_OK;
}